package com.xxx.course.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.xxx.course.common.Result;
import com.xxx.course.entity.EduCourse;
import com.xxx.course.entity.EduTeacher;
import com.xxx.course.entity.course.Course;
import com.xxx.course.entity.vo.CourseQuery;
import com.xxx.course.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author chn
 * @since 2022-05-29
 */
@RestController
@RequestMapping("/course")
@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;
    @GetMapping("/get")
    public Result get(){
        List<EduCourse> list = courseService.list(null);
        return Result.ok().data("course",list);
    }
    @PostMapping("/page/condition/{current}/{limit}")
    public Result pageCondition(@PathVariable("current") long current
                                , @PathVariable("limit") long limit
                                , @RequestBody CourseQuery courseQuery){
        Page<EduCourse> page = new Page<>(current, limit);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        String begin = courseQuery.getBegin();
        String end = courseQuery.getEnd();
        if (!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_modified",end);
        }
        courseService.page(page,wrapper);
        return Result.ok().data("page",page);
    }
    @PostMapping("update")
    public Result update(@RequestBody EduCourse eduCourse){
        return courseService.updateById(eduCourse)?Result.ok():Result.error();
    }
    @GetMapping("/get/{id}")
    public Result get(@PathVariable("id") String id){
        EduCourse course = courseService.getById(id);
        return Result.ok().data("course",course);
    }

    /**
     * 查询课程目录
     * @return
     */
    @GetMapping("/getCourse")
    public Result getCourse(){
        List<Course> course = courseService.getCourse();
        return Result.ok().data("course",course);
    }
    @PostMapping("/add")
    public Result add(@RequestBody EduCourse course){
        return courseService.save(course)?Result.ok():Result.error();
    }
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable String id){
        return courseService.removeById(id)?Result.ok():Result.error();
    }
}

